package com.kjlink.internal.workreport.system.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.Remind;
import cn.xuqiudong.common.util.CookieUtil;
import com.kjlink.internal.workreport.base.constant.CommonConstant;
import com.kjlink.internal.workreport.config.CommonProperties;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.system.service.SystemService;
import com.kjlink.internal.workreport.user.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述:
 *
 * @author Vic.xu
 * @since 2024-04-30 10:16
 */
@Controller
public class SystemController {


    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected HttpServletResponse response;

    @Resource
    private SystemService systemService;

    @Resource
    private CommonProperties commonProperties;

    @GetMapping(value = {"", "/"})
    public String root() {
//        return "redirect:login";
        return "redirect:/v3/index.html";
    }

    /**
     * 首页
     *
     * @return
     */
    @GetMapping(value = "index")
    public String index(Model model) {
        if (!commonProperties.isEnableOld()) {
            return "redirect:/v3/index.html";
        }
        return "index";
    }

    @GetMapping(value = "oldIndex")
    public String oldIndex(Model model) {
        return "index";
    }

    /**
     * 前往登录页面
     *
     * @return
     */
    @GetMapping(value = "/login")
    public String toLogin() {
        User user = systemService.ifLogin(request);
        if (user != null) {
            SessionHelper.storeUser(user);
            return "redirect:/index";
        }
        return "login";
    }

    /**
     * 登录
     *
     * @param userCode   userCode
     * @param password   password
     * @param rememberMe remomber me
     * @param attributes none
     * @return
     */
    @PostMapping(value = "/login")
    public String login(String userCode, String password, @RequestParam(required = false) boolean rememberMe,
                        RedirectAttributes attributes) {
        BaseResponse<User> response = systemService.login(userCode, password);
        if (response.getCode() != 0) {
            attributes.addFlashAttribute(Remind.warning().setMessage(response.getMsg()));
            return "redirect:login";
        }
        User user = response.getData();
        SessionHelper.storeUser(user);

        if (rememberMe) {
            String userToken = systemService.makeToken(user);
            CookieUtil.set(this.response, CommonConstant.PRINCIPAL, userToken, true);
        }
        attributes.addFlashAttribute(Remind.success());
        return "redirect:index";
    }

    /**
     * 退出
     *
     * @return
     */
    @GetMapping(value = "logout")
    public String logout() {
        SessionHelper.cleanSession();
        CookieUtil.remove(request, response, CommonConstant.PRINCIPAL);
        return "redirect:login";
    }

    @GetMapping("test")
    public String test(@RequestParam(defaultValue = "this is a variable") String test, Model model) {
        model.addAttribute("test", test);
        System.out.println("go to test page");
        return "test";
    }
}
